package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;

import static com.twu.biblioteca.InputReader.*;

public class BibliotecaApp {

    private static Integer loginOrExit(){
        Integer returnValue;
        System.out.println("1.Customer Login\n2.Librarian Login\n3.Exit System");
        try {
            returnValue=Integer.parseInt(readInput());
        }
        catch (NumberFormatException e)
        {
            returnValue=0;
        }
        return returnValue;
    }

    private static void populateDataStructure(List<Book> bookList,List<Movie> movieList,List<String> customerNameList,List<String> customerNumberList,List<String> passwordList,List<String> emailIDList,List<String> mobileNOList){

        bookList.add(new Book("Book1",2,2));
        bookList.add(new Book("Book2",1,1));
        bookList.add(new Book("Book3",3,3));
        bookList.add(new Book("Book4",7,7));
        bookList.add(new Book("Book5",5,5));

        movieList.add(new Movie("Movie1","Director1",2001,7,2,2));
        movieList.add(new Movie("Movie2","Director2",2011,null,1,1));

        customerNameList.add("customer1");
        customerNameList.add("customer2");
        customerNameList.add("customer3");

        customerNumberList.add("bng-cus1");
        customerNumberList.add("bng-cus2");
        customerNumberList.add("bng-cus3");

        passwordList.add("pass1");
        passwordList.add("pass2");
        passwordList.add("pass3");

        emailIDList.add("cus1@bngLib.in");
        emailIDList.add("cus2@bngLib.in");
        emailIDList.add("cus3@bngLib.in");

        mobileNOList.add("9874630080");
        mobileNOList.add("8867593108");
        mobileNOList.add("9901976903");
    }

    public static void main(String[] args) {

        System.out.println("Welcome to Bangalore Public Library!");

        Integer returnValue;
        String customerNumber,password;

        List<Book> bookList=new ArrayList<Book>();
        List<Movie> movieList=new ArrayList<Movie>();
        List<String> customerNameList=new ArrayList<String>();
        List<String> customerNumberList=new ArrayList<String>();
        List<String> passwordList=new ArrayList<String>();
        List<String> emailIDList=new ArrayList<String>();
        List<String> mobileNOList=new ArrayList<String>();

        populateDataStructure(bookList,movieList,customerNameList,customerNumberList,passwordList,emailIDList,mobileNOList);

        Library library=new Library(bookList,movieList);
        CustomerDB customerDB=new CustomerDB(customerNameList,customerNumberList,passwordList,emailIDList,mobileNOList);

        boolean invalidInput;
        Integer selectedMainMenuOption;
        String userInput;
        String queriedBookOrMovie=null;
        Integer verifiedUserDataIndexInCustomerDB;


        do {
            returnValue=loginOrExit();
            if(returnValue!=1 && returnValue!=2 && returnValue!=3)
            {
                System.out.println("Please select a valid option!");
                continue;
            }

            if(returnValue==1)
            {
                System.out.print("Enter customer number : ");
                customerNumber = readInput();
                System.out.print("Enter Password : ");
                password = readInput();
                verifiedUserDataIndexInCustomerDB=customerDB.verifyCustomerAndReturnIndexInCustomerDB(customerNumber,password);
                if (verifiedUserDataIndexInCustomerDB==null)
                    System.out.println("Invalid customer-number/ password!");
                else
                {
                    System.out.println("Hi! You have logged in Successfully!");
                    Customer customer=new Customer(customerNumber);
                    String currentUserInformation=customerDB.displayVerifiedUserInformation(verifiedUserDataIndexInCustomerDB);
                    do {
                        do {
                            System.out.println(customer.showMainMenu());
                            System.out.println("Select an option!");
                            userInput=customer.chooseMainMenuOption();
                            selectedMainMenuOption=customer.validateMainMenuOption(userInput);
                            if(selectedMainMenuOption==null)
                            {
                                System.out.println("Not a valid option!");
                                invalidInput=true;
                            }
                            else
                                invalidInput=false;
                        }while(invalidInput);

                        queriedBookOrMovie=null;
                        if(selectedMainMenuOption==2 || selectedMainMenuOption==3)
                        {
                            System.out.print("Enter book name : ");
                            queriedBookOrMovie=readInput();
                        }
                        if(selectedMainMenuOption==5 || selectedMainMenuOption==6)
                        {
                            System.out.print("Enter movie name : ");
                            queriedBookOrMovie=readInput();
                        }

                        String customerRequestStatus=processCustomerRequest(customer,library,selectedMainMenuOption,queriedBookOrMovie,currentUserInformation);
                        System.out.println(customerRequestStatus);

                    }while(selectedMainMenuOption!=8);
                }
            }
            else if(returnValue==2)
            {
                Librarian librarian=new Librarian(library);
                System.out.print("Enter Librarian Password : ");
                if (librarian.librarianLogin(readInput()))
                {
                    System.out.print("Enter the Book/ Movie name to check the borrower list : ");
                    System.out.println(librarian.displayBorrowersOfAParticularBookOrMovie(readInput()));
                }
                else
                    System.out.println("Librarian's Password Invalid!");
            }

        }while(returnValue!=3);
        System.out.println("System is shutting down...");
        System.out.println("Shut down completed!");
        }



    public static String processCustomerRequest(Customer customer,Library library, Integer selectedMainMenuOption,String queriedBookOrMovie,String currentUserInformation)
    {
        String customerRequestStatus="";
        switch (selectedMainMenuOption)
        {
            case 1:
                List<Book> bookList=library.getBookList();
                for(Book book:bookList)
                {
                    if(book.getCurrentQuantity()>0)
                        customerRequestStatus+=book.getBookName()+"\n";
                }
                customerRequestStatus+="Book list display request processed!";
                break;
            case 2:
                String bookToCheckout = queriedBookOrMovie;
                customerRequestStatus=customer.checkoutBook(library, bookToCheckout)+"\n";
                customerRequestStatus+="Book checkout request processed!";
                break;
            case 3:
                String bookToReturn = queriedBookOrMovie;
                customerRequestStatus=customer.returnBook(library, bookToReturn)+"\n";
                customerRequestStatus+="Book return request processed!";
                break;
            case 4:
                List<Movie> movieList=library.getMovieList();
                for(Movie movie:movieList)
                {
                    if(movie.getCurrentQuantity()>0)
                        customerRequestStatus+=movie.displayMovieInformation()+"\n";
                }
                customerRequestStatus+="Movie list display request processed!";
                break;
            case 5:
                String movieToCheckout = queriedBookOrMovie;
                customerRequestStatus=customer.checkoutMovie(library,movieToCheckout)+"\n";
                customerRequestStatus+="Movie checkout request processed!";
                break;
            case 6:
                String movieToReturn = queriedBookOrMovie;
                customerRequestStatus=customer.returnMovie(library,movieToReturn)+"\n";
                customerRequestStatus+="Movie return request processed!";
                break;
            case 7:
                customerRequestStatus=currentUserInformation;
                customerRequestStatus+="Current user information display request processed!";
                break;
            case 8:
                customerRequestStatus=customer.logout();
                customerRequestStatus+="\nLogout request processed!";
        }
        return customerRequestStatus;
    }
}
